package c.x.jy.service.service.impl;

import c.x.jy.dao.dto.OrderEvaluateEntityDto;
import c.x.jy.dao.entity.OrderEntity;
import c.x.jy.dao.entity.OrderItemsEntity;
import c.x.jy.service.service.OrderItemsService;
import c.x.jy.service.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import c.x.jy.common.utils.PageUtils;
import c.x.jy.common.utils.Query;

import c.x.jy.dao.dao.OrderEvaluateDao;
import c.x.jy.dao.entity.OrderEvaluateEntity;
import c.x.jy.service.service.OrderEvaluateService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;


@Service("orderEvaluateService")
public class OrderEvaluateServiceImpl extends ServiceImpl<OrderEvaluateDao, OrderEvaluateEntity> implements OrderEvaluateService {

    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderItemsService orderItemsService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        	QueryWrapper<OrderEvaluateEntityDto> query= 	 new QueryWrapper<>() ;
    	if(params.get("searchKey")!=null&&params.get("searchValue")!=null&&!StringUtils.isEmpty(params.get("searchKey").toString())&&!StringUtils.isEmpty(params.get("searchValue").toString()))
    	    query.eq(  params.get("searchKey").toString(), params.get("searchValue"));
        if(params.get("businessId")!=null) 	query.eq("t.business_id", params.get("businessId"));
//        IPage<OrderEvaluateEntity> page = this.page(
//                new Query<OrderEvaluateEntity>().getPage(params),
//                 query
//        );
        IPage<OrderEvaluateEntityDto> page = getBaseMapper().querypage( new Query<OrderEvaluateEntityDto>().getPage(params),
                 query
        );
        return new PageUtils(page);
    }

    @Override
    @Transactional
    public void evaluate(OrderEvaluateEntity orderEvaluate) {
        Long orderId = orderEvaluate.getOrderId();
        OrderEntity orderEntity = orderService.getById(orderId);
        orderEvaluate.setMemberId(orderEntity.getMemberId());
        orderEvaluate.setTeacherId(orderEntity.getTeacherId());
        List<OrderItemsEntity> itemList = orderItemsService.selectListByOrderNo(orderId);
//        List<OrderItemsEntity> itemList = orderEntity.getItemlist();
        if(itemList!=null && itemList.size()>0)
            orderEvaluate.setItemId(itemList.get(0).getItemId());
        orderEvaluate.setServiceType(orderEntity.getServiceType());

        save(orderEvaluate);
        orderEntity.setStatus(3);
        orderService.updateById(orderEntity);
    }

    @Override
    public List<OrderEvaluateEntityDto> getByTeacherId(Long teacherId) {
        List<OrderEvaluateEntityDto> l=getBaseMapper().getByTeacherId(teacherId);
        return l;
    }
}
